package com.tutorial.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.tutorial.io.Resource;
import com.tutorial.pojo.Configuration;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.util.List;
import java.util.Properties;

/**
 * 数据库配置信息解析类
 *
 * @author: GXW
 * @since: 2025-10-28 16:11:55
 **/
public class XMLConfigBuilder {
    private Configuration configuration;

    public XMLConfigBuilder() {
        this.configuration = new Configuration();
    }

    public Configuration parseConfig(InputStream inputStream) throws Exception {
        Document document = new SAXReader().read(inputStream);
        Element configurationRootElement = document.getRootElement();
        // 解析数据源配置dataSource下的参数信息
        List<Element> elementList = configurationRootElement.selectNodes("//property");
        Properties properties = new Properties();
        for (Element element : elementList) {
            String name = element.attributeValue("name");
            String value = element.attributeValue("value");
            properties.put(name, value);
        }

        // 使用c3p0数据源
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(properties.getProperty("driverClass"));
        dataSource.setJdbcUrl(properties.getProperty("jdbcUrl"));
        dataSource.setUser(properties.getProperty("userName"));
        dataSource.setPassword(properties.getProperty("password"));

        // 设置数据源
        configuration.setDataSource(dataSource);

        // 解析mapper.xml,根据路径读取字节输入流,使用dom4j进行解析
        List<Element> mapperElementList = configurationRootElement.selectNodes("//mapper");
        for (Element element : mapperElementList) {
            String mapperPath = element.attributeValue("resource");
            InputStream resourceAsStream = Resource.getResourceAsStream(mapperPath);
            XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(configuration);
            xmlMapperBuilder.parseMapper(resourceAsStream);
        }
        return configuration;
    }
}
